package cn.qu.phizline.webserver.api.v1.service.impl;

import cn.qu.phizline.common.TypeUtils;
import cn.qu.phizline.event.Payload;
import cn.qu.phizline.webserver.api.v1.service.Apiv1Service;
import cn.qu.phizline.webserver.event.handle.Handle;
import cn.qu.phizline.webserver.ws.WebServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

/**
 * Created by zh on 2017/9/18.
 */
@Service
public class Apiv1ServiceImpl implements Apiv1Service {

    @Autowired
    private WebServer webServer;
    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public Payload process(Payload payload) {
        Class<? extends Handle> handleType = webServer.getHandleType(payload.getType(), payload.getVersion());
        Handle handle = TypeUtils.newInstance(handleType);

        applicationContext.getAutowireCapableBeanFactory().autowireBean(handle);

        return handle.handle(payload);
    }
}
